import {Inject, Injectable} from '@angular/core';
import {Observable} from 'rxjs';
import { Singer} from './data-types/common.type';
import {map} from 'rxjs/internal/operators';
import {API_CONFIG, ServicesModule} from './services.module';
import {HttpClient, HttpParams} from '@angular/common/http';
import queryString from 'query-string';

type SingerParam= {
  offset: number;
  limit: number;
  cat?: string;
}
const defaultParam: SingerParam = {
  offset: 0,
  limit: 9,
  cat: '5001'
};

@Injectable({
  providedIn: ServicesModule
})
export class SingerService {
  constructor(private http: HttpClient, @Inject(API_CONFIG) private uri: string) {
  }

  getSinger(args: SingerParam = defaultParam): Observable<Singer[]> {
    const params = new HttpParams({fromString: queryString.stringify(args)});
    return this.http.get(this.uri + 'artist/list', {params}).pipe(map(
      (res: { artists: Singer[] }) => res.artists
    ));
  }
}
